from sqlalchemy import Column, Integer, String, Float, Boolean, Text, ForeignKey
from werkzeug.security import generate_password_hash, check_password_hash
from common.base_model import BaseModel, db

class User(BaseModel):
    __tablename__ = 'user_info'
    id = db.Column(db.Integer, autoincrement=True, primary_key=True)
    name = db.Column(db.String(10), unique=True, nullable=False)
    email = db.Column(db.String(50), unique=True, nullable=False)
    age = db.Column(db.Integer, default=0)
    role = db.Column(db.String(50), nullable=False, default="user", unique=False)
    password_hash = db.Column(db.String(128))

    @property
    def password(self):
        raise AttributeError('密码不可读')

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __str__(self):
        return self.name
    
    def to_dict(self, exclude_field=['password_hash', ]):
        return super().to_dict(exclude_field)


if __name__=="__main__":
    #  对表的操作 创建：# 删除：
    # db.drop_all()
    # db.create_all()
    user1 = User.query.filter_by(id=1).first()
    # user1.save()
    print(user1)